package org.alwayssuper.protocol.commons.transform.realtimev2025;

import org.alwayssuper.protostar.annotation.Field;
import lombok.Data;
import lombok.ToString;
import lombok.experimental.Accessors;

/**
 * 2025版燃料电池发动机及车载氢系统数据格式 (0x03)
 * 参考GB/T 32960.3-2025标准表17
 * @author alwaysSuper
 */
@ToString
@Data
@Accessors(chain = true)
public class FuelCellEngineData {
    @Field(length = 2, desc = "车载氢系统中最高温度:有效值范围0~250(偏移量40℃)")
    private int maxTemperatureInHydrogenSystem;
    
    @Field(length = 1, desc = "车载氢系统中最高温度探针代号:有效值范围1~253")
    private int maxTemperatureProbeNo;
    
    @Field(length = 2, desc = "氢气最高浓度:有效值范围0~60000(表示体积分数0%~6%)")
    private int maxHydrogenConcentration;
    
    @Field(length = 1, desc = "氢气最高浓度传感器代号:有效值范围1~253")
    private int maxHydrogenConcentrationSensorNo;
    
    @Field(length = 2, desc = "氢气最高压力:有效值范围0~1000(表示0MPa~100MPa)")
    private int maxHydrogenPressure;
    
    @Field(length = 1, desc = "氢气最高压力传感器代号:有效值范围1~253")
    private int maxHydrogenPressureSensorNo;
    
    @Field(length = 1, desc = "高压DC/DC状态:0x01:工作;0x02:断开")
    private int highVoltageDcDcStatus;
    
    @Field(length = 1, desc = "剩余氢量百分比:有效值范围0~100(表示0%~100%)")
    private int remainingHydrogenPercentage;
    
    @Field(length = 1, desc = "高压DC/DC控制器温度:0~250(偏移量40℃)")
    private int dcDcControllerTemperature;
    
    // ==================== 传输值与真实值转换方法 ====================
    
    /**
     * 获取氢系统最高温度 (℃)
     * @return 温度，精度1℃，异常返回-999，无效返回-1000
     */
    public int getMaxTemperatureInHydrogenSystem() {
        if (maxTemperatureInHydrogenSystem == 0xFFFE) return -999; // 异常
        if (maxTemperatureInHydrogenSystem == 0xFFFF) return -1000; // 无效
        return maxTemperatureInHydrogenSystem - 40; // (传输值-40) = 值(℃)，支持负值
    }
    
    /**
     * 设置氢系统最高温度 (℃)
     * @param temperature 温度值，范围-40℃~+210℃
     */
    public void setMaxTemperatureInHydrogenSystem(int temperature) {
        if (temperature < -40 || temperature > 210) {
            this.maxTemperatureInHydrogenSystem = 0xFFFF; // 无效值
        } else {
            this.maxTemperatureInHydrogenSystem = temperature + 40; // (值+40) = 传输值
        }
    }
    
    /**
     * 获取氢气最高浓度 (%)
     * @return 浓度，精度0.01%，异常返回-1，无效返回-2
     */
    public double getMaxHydrogenConcentration() {
        if (maxHydrogenConcentration == 0xFFFE) return -1.0; // 异常
        if (maxHydrogenConcentration == 0xFFFF) return -2.0; // 无效
        return maxHydrogenConcentration * 0.01; // 传输值×0.01 = 值(%)
    }
    
    /**
     * 设置氢气最高浓度 (%)
     * @param concentration 浓度值，范围0~6%
     */
    public void setMaxHydrogenConcentration(double concentration) {
        if (concentration < 0 || concentration > 6.0) {
            this.maxHydrogenConcentration = 0xFFFF; // 无效值
        } else {
            this.maxHydrogenConcentration = (int) Math.round(concentration * 100); // 值×100 = 传输值
        }
    }
    
    /**
     * 获取氢气最高压力 (MPa)
     * @return 压力，精度0.1MPa，异常返回-1，无效返回-2
     */
    public double getMaxHydrogenPressure() {
        if (maxHydrogenPressure == 0xFFFE) return -1.0; // 异常
        if (maxHydrogenPressure == 0xFFFF) return -2.0; // 无效
        return maxHydrogenPressure * 0.1; // 传输值×0.1 = 值(MPa)
    }
    
    /**
     * 设置氢气最高压力 (MPa)
     * @param pressure 压力值，范围0~100MPa
     */
    public void setMaxHydrogenPressure(double pressure) {
        if (pressure < 0 || pressure > 100.0) {
            this.maxHydrogenPressure = 0xFFFF; // 无效值
        } else {
            this.maxHydrogenPressure = (int) Math.round(pressure * 10); // 值×10 = 传输值
        }
    }
    
    /**
     * 获取剩余氢量百分比 (%)
     * @return 百分比，精度1%，异常返回-1，无效返回-2
     */
    public int getRemainingHydrogenPercentage() {
        if (remainingHydrogenPercentage == 0xFE) return -1; // 异常
        if (remainingHydrogenPercentage == 0xFF) return -2; // 无效
        return remainingHydrogenPercentage; // 传输值 = 值(%)
    }
    
    /**
     * 设置剩余氢量百分比 (%)
     * @param percentage 百分比，范围0~100%
     */
    public void setRemainingHydrogenPercentage(int percentage) {
        if (percentage < 0 || percentage > 100) {
            this.remainingHydrogenPercentage = 0xFF; // 无效值
        } else {
            this.remainingHydrogenPercentage = percentage; // 值 = 传输值
        }
    }
    
    /**
     * 获取DC/DC控制器温度 (℃)
     * @return 温度，精度1℃，异常返回-999，无效返回-1000
     */
    public int getDcDcControllerTemperature() {
        if (dcDcControllerTemperature == 0xFE) return -999; // 异常
        if (dcDcControllerTemperature == 0xFF) return -1000; // 无效
        return dcDcControllerTemperature - 40; // (传输值-40) = 值(℃)，支持负值
    }
    
    /**
     * 设置DC/DC控制器温度 (℃)
     * @param temperature 温度值，范围-40℃~+210℃
     */
    public void setDcDcControllerTemperature(int temperature) {
        if (temperature < -40 || temperature > 210) {
            this.dcDcControllerTemperature = 0xFF; // 无效值
        } else {
            this.dcDcControllerTemperature = temperature + 40; // (值+40) = 传输值
        }
    }
    
    /**
     * 检查氢系统最高温度是否有效
     * @return true: 有效, false: 异常或无效
     */
    public boolean isMaxTemperatureInHydrogenSystemValid() {
        return maxTemperatureInHydrogenSystem != 0xFFFE && maxTemperatureInHydrogenSystem != 0xFFFF;
    }
    
    /**
     * 检查氢气最高浓度是否有效
     * @return true: 有效, false: 异常或无效
     */
    public boolean isMaxHydrogenConcentrationValid() {
        return maxHydrogenConcentration != 0xFFFE && maxHydrogenConcentration != 0xFFFF;
    }
    
    /**
     * 检查氢气最高压力是否有效
     * @return true: 有效, false: 异常或无效
     */
    public boolean isMaxHydrogenPressureValid() {
        return maxHydrogenPressure != 0xFFFE && maxHydrogenPressure != 0xFFFF;
    }
    
    /**
     * 检查剩余氢量百分比是否有效
     * @return true: 有效, false: 异常或无效
     */
    public boolean isRemainingHydrogenPercentageValid() {
        return remainingHydrogenPercentage != 0xFE && remainingHydrogenPercentage != 0xFF;
    }
    
    /**
     * 检查DC/DC控制器温度是否有效
     * @return true: 有效, false: 异常或无效
     */
    public boolean isDcDcControllerTemperatureValid() {
        return dcDcControllerTemperature != 0xFE && dcDcControllerTemperature != 0xFF;
    }
}